import "../bootstrap";
import ElementUI from "element-ui";
import "element-ui/lib/theme-default/index.css";

Vue.use(ElementUI); //注册饿了么组件

var app = new Vue({
    el: "#app",
    data: {
        loading: false,
        tip: '',
        form: {
            username: '',
            userpass: '',
            reuserpass: '',
            safepass: '',
            resafepass: '',
            pname: window.def.pname,
            nname: window.def.nname,
            bdzx: window.def.bdzx,
            vip: ''
        },
        rules: {
            username: [
                {required: true, message: '请输入用户账号', trigger: 'blur'},
                {min: 5, max: 32, message: '长度在 5 到 32 个字符', trigger: 'blur'}
            ],
            vip: [
                {required: true, message: '请选择用户等级', trigger: 'change'},
            ],
            userpass: [
                {required: true, message: '请输入登录密码', trigger: 'blur'},
                {min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur'}
            ],
            reuserpass: [
                {validator: equalTo, equal: "userpass", message: '两次密码不一致！', trigger: 'blur'}
            ],
            safepass: [
                {required: true, message: '请输入交易密码', trigger: 'blur'},
                {min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur'}
            ],
            resafepass: [
                {validator: equalTo, equal: "safepass", message: '两次密码不一致！', trigger: 'blur'}
            ],
            pname: [
                {required: true, message: '请输入推荐人账号', trigger: 'blur'},
                {min: 5, max: 32, message: '长度在 5 到 32 个字符', trigger: 'blur'}
            ],
            nname: [
                {required: true, message: '请输入节点人账号', trigger: 'blur'},
                {min: 5, max: 32, message: '长度在 5 到 32 个字符', trigger: 'blur'}
            ],
            bdzx: [
                {required: true, message: '请输入报单中心账号', trigger: 'blur'},
                {min: 5, max: 32, message: '长度在 5 到 32 个字符', trigger: 'blur'}
            ]
        }
    },
    methods: {
        onSubmit: function () {
            this.loading = true;
            this.tip = false;
            let self = this;
            this.$refs['ruleForm'].validate(function (valid) {
                if (valid) {    //表单检查通过才提交表单
                    self.$http.post('/reg-act', self.form).then(
                        function (response) {       //请求成功
                            self.loading = false;
                            self.tip = response.body.msg;
                            if (response.body.code == 0) {
                                setTimeout(function () {
                                    location.href = "/login";
                                }, 2000);
                            }
                        }, function (response) {    //请求失败
                            self.loading = false;
                            self.tip = response.statusText;
                        });
                } else {
                    self.loading = false;
                    return false;
                }
            });
        },
    }
});
/**
 * 表单相等效验
 */
function equalTo(rule, value, callback) {
    if (value !== app.$data.form[rule.equal]) {
        callback(new Error(rule.message));
    } else {
        callback();
    }
};